% analyCascadeFineTuning2
clear
% ResultPath = '/home/wks/SR-Works/warmStarting';

% Scale_Path = {
%     2, '/home/wks/SR-Works/warmStarting/Scale2/VDSR_y-1'
%     3, '/home/wks/SR-Works/warmStarting/Scale3/VDSR_y-1'
    % 4, '/home/wks/SR-Works/warmStarting/Scale4/VDSR_y-2'
    % 4, '/home/wks/SR-Works/warmStarting/Scale4/[VDSR_y-1]-tuning2-[VDSR_y-1]'
    % 4, '/home/wks/SR-Works/warmStarting/Scale4/[VDSR_y-1]-tuning2-[VDSR_y-2]'
    % 4, '/home/wks/SR-Works/warmStarting/Scale4/[VDSR_y-1]-tuning2-[VDSR_y-3]'
%     };

% for ri = 1 : size(Scale_Path, 1)
%     [scale, ResultPath] = Scale_Path{ri, :};
%     ImgPairs0 = srimg.genLHPairs(set, scale);
%     ModelPath = fullfile(ResultPath, 'models');
%     QualityList = srdata.deployImgsByIterModels2(set, ImgPairs0(:, 2:3), ModelPath, solver_mode);
% end

scale = 3;
ResultPath = '/home/wks/SR-Works/warmStarting/Scale3';
Folders = dir(ResultPath);
Folders = {Folders(3 : end).name};
Bool = cell2mat(cellfun(@isdir, Folders, 'uniformoutput', false));
Folders = Folders(Bool);

set = 'Set5';
solver_mode = 'gpu 0';

ImgPairs0 = srimg.genLHPairs(set, scale);

% for fi = 1 : length(Folders)
%     ModelPath = fullfile(ResultPath, Folders{fi}, 'models');
%     QualityList = srdata.deployImgsByIterModels2(set, ImgPairs0(:, 2:3), ModelPath, solver_mode);
% end

ModelPath = '/home/wks/SR-Works/warmStarting/Scale3/VDSR_y-2/models_clip0.01';
QualityList = srdata.deployImgsByIterModels2(set, ImgPairs0(:, 2:3), ModelPath, solver_mode);
return

%%
ParamList = {
%     2, 7, 1500
%     4, 4, 500
%     4, 5, 1000
%     4, 6, 1500
%     3, 1, 500
%     3, 2, 1000
%     3, 3, 1500
    
%     2, 8, 1500
%     2, 9, 3000
%     2, 10, 6000
    
%     2, 11, 1500
%     2, 12, 3000
%     2, 13, 6000
%     
%     2, 14, 1500
%     2, 15, 3000
%     2, 16, 6000
%     
%     3, 4, 500
%     3, 11, 3000
%     3, 4, 1500
%     3, 4, 1500
%     3, 5, 3000
%     3, 6, 6000
%     
%     3, 7, 1500
%     3, 8, 3000
%     3, 9, 6000
%     
%     3, 10, 1500
%     3, 11, 3000
%     3, 12, 6000
    
%     4, 7, 1500
%     4, 8, 3000
%     4, 9, 6000
%     
%     4, 10, 1500
%     4, 11, 3000
%     4, 12, 6000
%     
%     4, 13, 1500
%     4, 14, 3000
%     4, 15, 6000
    };

QualityList = cell(size(ParamList, 1), 3);
for pi = 1 : size(ParamList, 1)
    [scale, fileOrder, startIter] = ParamList{pi, :};
    ImgPairs0 = srimg.genLHPairs('Set5', scale);
    
    ModelPath = fullfile(ExpPath, sprintf('Scale%d', scale), 'VDSR_y-1/models');
    [~, QualityList{pi, :}] = srdata.deployImgsByIterModels(ImgPairs0(:, 2:3), scale, ModelPath);
    save(fullfile(ResultPath, sprintf('QualityList-MSRA-Scale%d.mat', scale)), 'QualityList');
    
    % ModelPath = sprintf('/home/wks/SR-Works/%%CascadeFineTuningScale%d/VDSR_y-1/models', scale);
    % ImgPairs = srdata.deployImgsByIterModels(ImgPairs0(:, 2:3), scale, ModelPath, startIter);
    
%     ModelPath = sprintf('/home/wks/SR-Works/%%CascadeFineTuningScale%d/[VDSR_y-1]-tuning2-[VDSR_y-%d]/models', scale, fileOrder);
%     if startIter == 1500, ModelPath = sprintf('%s_1500', ModelPath); end
%     
%     [~, QualityList{pi, :}] = srdata.deployImgsByIterModels(ImgPairs, scale, ModelPath, 100:100:6000);
%     save(fullfile(ResultPath, 'QualityList-WarmStarting-S3-1500-clip0.05.mat'), 'QualityList');
end

return

%%
% Scales = [2 3 4];
% StartPoints = 0 : 500 : 1500;
% ImgList = cell(length(Scales), 5, 4);
% for si = 1 : length(Scales)
%     scale = Scales(si);
%     ImgPairs0 = srimg.genLHPairs('Set5', scale);
%     ImgList(si, :, 1) = ImgPairs0(:, 2);
%     for pi = 2 : length(StartPoints)
%         startIter = StartPoints(pi);
%         ModelPath = sprintf('/home/wks/SR-Works/%%CascadeFineTuningScale%d/VDSR_y-1/models', scale);
%         ImgPairs = srdata.deployImgsByIterModels(ImgPairs0(:, 2:3), scale, ModelPath, startIter);
%         ImgList(si, :, pi) = ImgPairs(:, 1);
%     end
% end

%%
load('ImgList.mat')
StartNum = 4;
close all
ki = 3;
for si = 1 : 3
    h = figure;
    hAxes = tight_subplot(1, StartNum-1);
    for pi = 1 : StartNum
        img = ImgList{si, ki, pi};
        if pi == 1, img0 = img; continue; end
        h.CurrentAxes = hAxes(pi-1);
        imshow(abs(img - img0), [])
        img0 = img;
    end
    colormap hot;
end
return

%%
scale = 3;
ImgPairs0 = srimg.genLHPairs('Set5', scale);
ImageQualities3_1 = cell(1, 3);
ModelPath = '/home/wks/SR-Works/%CascadeFineTuningScale3/VDSR_y-1/models';
[~, ImageQualities3_1{:}] = srdata.deployImgsByIterModels(ImgPairs0(:, 2:3), scale, ModelPath, 3100 : 100 : 6000);

mycaffe.logFile(fullfile(ResultPath, 'log.txt'), sprintf('scale = %d, startpoint = 0', scale));
save(fullfile(ResultPath, 'ImageQualities3_1.mat'), 'ImageQualities3_1');

%%
scales = [2 3 4];
iters = [500 1000 1500];
ImageQualities = cell(1 + length(scales), 3, length(iters));

for si = 1 : length(scales);
    scale = scales(si);
    ImgPairs0 = srimg.genLHPairs('Set5', scale);
    for i = 1 : length(iters)
        ModelPath = sprintf('/home/wks/SR-Works/%%CascadeFineTuningScale%d/VDSR_y-1/models', scale);
        ImgPairs = srdata.deployImgsByIterModels(ImgPairs0(:, 2:3), scale, ModelPath, iters(i));
        
        ModelPath = sprintf('/home/wks/SR-Works/%%CascadeFineTuningScale%d/[VDSR_y-1]-tuning2-[VDSR_y-%d]/models', scale, i);
        [~, ImageQualities{si+1, :, i}] = srdata.deployImgsByIterModels(ImgPairs, scale, ModelPath);
        
        save(fullfile(ResultPath, 'ImageQualities.mat'), 'ImageQualities');
        
        mycaffe.logFile(fullfile(ResultPath, 'log.txt'), sprintf('scale = %d, startpoint = %d', scale, iters(i)));
    end
end
